﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;


namespace DARE
{
    /// <summary>
    /// This represents the main abstract class for the scrollbars.
    /// </summary>
    /// <seealso cref="CHorizontalScrollbar"/>
    /// <seealso cref="CVerticalScrollbar"/>
    [AttrTypeDependentSerializable(typeof(CScrollBarData))]
    public abstract class CScrollBar: CWidget
    {
        #region fields

        protected CButton m_lessButton;
        protected CButton m_moreButton;
        protected CButton m_slider;
        
        protected float m_incrementor;

        protected float m_value;

        protected bool m_isLessClicked = false;
        protected bool m_isMoreClicked = false;

        protected double m_lastRepeated = 0;
        protected double m_repeatInterval = 500;
        protected bool m_repeatEnabled = false;

        #endregion

        #region properties

        /// <summary>
        /// Gets/Sets the incrementation value. This value is the percentage of the total size incremented at each click on the buttons.
        /// For example, for a vertical scrollbar, there are two buttons, an button to scroll up and a button to scroll down.
        /// This value is the amount that each click on those buttons apply on the slider.
        /// </summary>
        public float Incrementation { get { return m_incrementor; } set { m_incrementor = m_value; if (m_value == 100) m_incrementor = 0; } }

        /// <summary>
        /// Gets/Sets the position (in percent) of the slider.
        /// </summary>
        public float Value { get { return m_value; } set { this.m_value = value; updateSliderPos(); } }

        /// <summary>
        /// [Deprecated]
        /// Gets/Sets the slider size
        /// </summary>
        public Vector2 SliderSize { get { return m_slider.Size; } set { m_slider.Size = value; m_slider.Actualize(); updateSliderPos(); } }

        #endregion

        #region ctor

        public CScrollBar(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_value = 0;
            m_incrementor = 1;
            m_lessButton = new CButton(hud, this, name + "_lessButton");
            m_moreButton = new CButton(hud, this, name + "_moreButton");
            m_slider = new CButton(hud, this, name + "_slider");

            m_lessButton.Position = new Vector2(0, 0);
            m_lessButton.OnClick += lessClicked;

            m_moreButton.OnClick += moreClicked;



        }

        #endregion

        #region event

        /// <summary>
        /// Method called each time the less button is clicked on
        /// </summary>
        public void lessClicked(CWidget sender)
        {
            if ((CDare.Instance.InputMgr.Mouse.ButtonPressedEvent.Count > 0 && CDare.Instance.InputMgr.Mouse.ButtonPressedEvent.First() == CMouse.EButton.LEFT) || m_repeatEnabled)
            {
                m_value -= m_incrementor;
                if (m_value < 0)
                {
                    m_value = 0;
                    return;
                }
                updateSliderPos();

                m_isLessClicked = true;
            }
        }

        /// <summary>
        /// method called each time the more button is clicked on
        /// </summary>
        public void moreClicked(CWidget sender)
        {
            if ((CDare.Instance.InputMgr.Mouse.ButtonPressedEvent.Count > 0 && CDare.Instance.InputMgr.Mouse.ButtonPressedEvent.First() == CMouse.EButton.LEFT) || m_repeatEnabled)
            {
                m_value += m_incrementor;
                if (m_value > 100)
                {
                    m_value = 100;
                    return;
                }
                updateSliderPos();
                //m_repeatInterval = 500;
                m_isMoreClicked = true;
            }
        }

        /// <summary>
        /// Method called whenever the mouse is pressed and is over this widget
        /// </summary>
        public override bool onClick(CMouse mouse)
        {
            if (!m_lessButton.isMouseOver(mouse) && !m_moreButton.isMouseOver(mouse))
            {
                m_value = getRelativeMousePos(mouse);
                updateSliderPos();
            }
            
            return base.onClick(mouse);
        }

        /// <summary>
        /// Method called whenever the mouse is released and is over this widget and was pressed over this widget
        /// </summary>
        public override bool onRelease(CMouse mouse)
        {
            if (m_slider.IsLeftClicked)
            {
                m_slider.onRelease(mouse);
                m_slider.IsLeftClicked = false;
                updateSliderPos();
            }
            if (m_isLessClicked)
                m_lessButton.onRelease(mouse);
            if (m_isMoreClicked)
                m_moreButton.onRelease(mouse);

            m_isLessClicked = false;
            m_isMoreClicked = false;
            m_repeatInterval = 500;
            m_repeatEnabled = false;
            return base.onRelease(mouse);
        }

        #endregion

        #region update

        /// <summary>
        /// Called automatically.
        /// Used to update the slider position.
        /// </summary>
        public abstract void updateSliderPos();

        protected abstract float getRelativeMousePos(CMouse mouse);

        #endregion
    }
}
